package site.lyweb.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import site.lyweb.constant.Constant;
import site.lyweb.domain.dto.PageDTO;
import site.lyweb.domain.dto.Teacher;
import site.lyweb.domain.dto.TeacherDTO;
import site.lyweb.domain.query.TeacherQuery;
import site.lyweb.mapper.TeacherMapper;
import site.lyweb.service.TeacherService;
import site.lyweb.utils.ExtBeanUtils;

import java.lang.annotation.Target;
import java.util.List;

/**
* @author 深山老林
* @description 针对表【teacher】的数据库操作Service实现
* @createDate 2024-06-19 20:53:32
*/
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher>
    implements TeacherService {
    @Resource
    private TeacherMapper teacherMapper;
    /**
     * 分页 + 搜索
     *
     * @param current
     * @param teacherQuery
     * @return
     */
    @Override
    public PageDTO<TeacherDTO> getTeacherByPage(Integer current, TeacherQuery teacherQuery) {
        //分页查询分两个查询，1、查符合条件的数据总条数；2、查询当前页的数据
        int totalRow = teacherMapper.selectByCount(teacherQuery);
        int start = (current - 1) * Constant.PAGE_SIZE;
        List<Teacher> teacherList = teacherMapper.selectByPage(teacherQuery);
        int totalPage = totalRow / Constant.PAGE_SIZE;
        if (totalRow % Constant.PAGE_SIZE > 0) { //如果除不尽，总页数就需要加1
            totalPage ++;
        }
        //需要把 List<TUser> 转换为 List<UserInfoDTO>
        List<TeacherDTO> teacherDTOList = ExtBeanUtils.copyPropertiesForList(teacherList, TeacherDTO.class);

        return new PageDTO<>(current, Constant.PAGE_SIZE, totalRow, totalPage, teacherDTOList);

    }
}




